package net.sxnic.ugr.user.action;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.util.MvcUtils;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 用户Browse Action
 * 
 * @version $Revision$
 * @author 孙宇飞
 * @author 吕盛槐 update date : 2011-9-26
 */
@Controller
@RequestMapping(value = "/admin/ugr")
public class Browse extends UgrActionSupport {

	private Page users;

	@Autowired
	private UserManager userManager;

	/**
	 * 按名称模糊查询
	 */
	private String txtname;// 登录名
	private String queryUnit;// 单位

	// browse一般就这样的写法，因为有可能是post也有可能是get
	@RequestMapping(value = "user_browse.jspa", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String execute(String queryFullName, ModelMap model)
			throws Exception {
		try {
			checkRoles(Role.SYSTEM_ADMIN);

			int pageNumber = WebUtils.getPageNumber(request);
			int pageSize = PropertyUtil.findTableSize("firstbrowse.tabsize");

			HibernateOrder infoCodeOrder = HibernateDaoUtils
					.createHibernateOrder("userOrder", true);
			HibernateCriteria hc = new HibernateCriteria().add(infoCodeOrder);

			Criterion dc = null;
			Criterion dc1 = null;

			dc = Restrictions.ne("username", "admin");
			dc1 = Restrictions.eq("enabled", true);
			hc.add(dc);
			hc.add(dc1);

			// 从页面上获取参数有两种方式
			// 1用request获取，如txtname。
			// 2在方法中定义一个参数，有springmvc帮你获取，如queryFullName

			// 组合条件
			txtname = request.getParameter("txtname");// 登录名
			if (StringUtils.isNotBlank(txtname)) {
				dc = Restrictions.like("username", "%" + txtname + "%");
				hc.add(dc);
			}
			if (StringUtils.isNotBlank(queryFullName)) {// 用户名
				dc = Restrictions.like("fullname", "%" + queryFullName + "%");
				hc.add(dc);
			}
			if (StringUtils.isNotBlank(queryUnit)) {// 用户名
				dc = Restrictions.like("dept.name", "%" + queryUnit + "%");
				hc.add(dc);
			}
			users = userManager.getPageByCriteria(pageNumber, pageSize, hc);

			model.addAttribute("users", users);

		} catch (Exception e) {
			e.printStackTrace();
		}

		// browse方法一般需要处理提示信息
		model.addAttribute("actMsg", MvcUtils.getMessage(request));

		// 转到页面之前还需要把查询条件加入到model
		model.addAttribute("txtname", txtname);
		model.addAttribute("queryFullName", queryFullName);

		return "admin/ugr/user/browse";
	}

	public Page getUsers() {
		return users;
	}

	public String getTxtname() {
		return txtname;
	}

	public void setTxtname(String txtname) {
		this.txtname = txtname;
	}

	public String getQueryUnit() {
		return queryUnit;
	}

	public void setQueryUnit(String queryUnit) {
		this.queryUnit = queryUnit;
	}
}
